package cn.aishua.ui.myKaka;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.SplashActivity;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;
import cn.aishua.util.Constant;
import cn.aishua.util.FormatConverter;

import cn.aishua.R;

public class RegisterActivity
    extends Activity
{

    private EditText etMobileNumber;

    private TextView tvMobileNumber;
    private EditText etAuthCode;
    private Button btnResend;

    private MyTimer timer = new MyTimer( 60000, 1000 );

    private String strMobileNumber;
    private String strCaptcha;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_register );
        initiate();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if( !SplashActivity.MAIN_IS_IN_STACK )
        {
            finish();
        }
    }

    private void initiate()
    {
        etMobileNumber = (EditText) findViewById( R.id.my_kaka_register_et_mobile_number );
    }

    class MyTimer
        extends CountDownTimer
    {
        public MyTimer(long millisInFuture, long countDownInterval)
        {
            super( millisInFuture, countDownInterval );
        }

        @Override
        public void onTick( long millisUntilFinished )
        {
            btnResend.setText( "重发验证码(" + millisUntilFinished / 1000 + ")" );
            btnResend.setClickable( false );
        }

        @Override
        public void onFinish()
        {
            btnResend.setText( "重发验证码" );
            btnResend.setClickable( true );
        }

    }

    public void sendCaptcha( View view )
    {
        strMobileNumber = etMobileNumber.getText().toString();
        if( strMobileNumber.length() != 11 )
        {
            Toast.makeText( this, "您输入的手机号不正确，请重新输入", Toast.LENGTH_SHORT ).show();
        }
        else
        {
            new DataExchangeAsyncTask( this, "获取短信验证码" )
            {

                @Override
                protected HttpResponse sendRequest( String... params )
                {
                    return HttpRequestUtil.getInstance().sendRegCaptcha( params );
                }

                @Override
                protected void handleSuccResponse( String strJson )
                {
                    Toast.makeText( RegisterActivity.this, "验证码已发送到" + strMobileNumber + "，请查收", Toast.LENGTH_SHORT )
                        .show();
                    setContentView( R.layout.my_kaka_register_after_get_auth_code );

                    tvMobileNumber = (TextView) findViewById( R.id.my_kaka_register_after_get_auth_code_tv_mobile_number );
                    tvMobileNumber.setText( strMobileNumber );
                    etAuthCode = (EditText) findViewById( R.id.my_kaka_register_after_get_auth_code_tv_auth_code );
                    btnResend = (Button) findViewById( R.id.my_kaka_register_after_get_auth_code_btn_resend );
                    timer.start();
                }
            }.execute( strMobileNumber );
        }
    }

    public void sendAuthCodeAgain( View view )
    {
        new DataExchangeAsyncTask( this, "获取短信验证码" )
        {

            @Override
            protected HttpResponse sendRequest( String... params )
            {
                return HttpRequestUtil.getInstance().sendRegCaptcha( params );
            }

            @Override
            protected void handleSuccResponse( String strJson )
            {
                Toast.makeText( RegisterActivity.this, "验证码已发送到" + strMobileNumber + "，请查收", Toast.LENGTH_SHORT )
                    .show();
                //                setContentView( R.layout.my_kaka_register_after_get_auth_code );
                timer.start();
            }
        }.execute( strMobileNumber );
    }

    public void submit( View view )
    {
        strCaptcha = etAuthCode.getText().toString();
        if( !FormatConverter.isNotEmptyString( strCaptcha ) )
        {
            Toast.makeText( this, "请输入验证码", Toast.LENGTH_SHORT ).show();
        }
        else
        {
            new DataExchangeAsyncTask( this, "提交验证码" )
            {

                @Override
                protected HttpResponse sendRequest( String... params )
                {
                    return HttpRequestUtil.getInstance().validateRegCapcha( params );
                }

                @Override
                protected void handleSuccResponse( String strJson )
                {
                    Intent intent = new Intent( context, RegisterNextActivity.class );
                    intent.putExtra( Constant.EXTRA_STR_MOBILE_NUMBER, strMobileNumber );
                    intent.putExtra( Constant.EXTRA_STR_CAPTCHA, strCaptcha );
                    startActivity( intent );
                }
            }.execute( strCaptcha, strMobileNumber );
        }
    }

    public void back( View view )
    {
        onBackPressed();
    }

    @Override
    protected void onPause()
    {
        Log.d( "RegisterActivity", "onPause()" );
        super.onPause();
        timer.cancel();
    }

}
